import { ElNotification } from 'element-plus'
import dayjs from 'dayjs'
type Unit = 'B' | 'KB' | 'MB' | 'GB'
/**
 * 计算文件大小并转换为适当的单位
 * @param bytes - 文件大小（字节）
 * @param decimals - 小数点位数，默认为 2
 * @returns 格式化后的文件大小字符串
 */
export const computedUnitSize = (bytes: number, decimals: number = 2): string => {
  if (!Number.isFinite(bytes) || bytes < 0) {
    return '0B'
  }
  const units: Unit[] = ['B', 'KB', 'MB', 'GB']
  const k = 1024
  const i = bytes === 0 ? 0 : Math.floor(Math.log(bytes) / Math.log(k))
  const unitIndex = Math.min(i, units.length - 1)
  const value = bytes / Math.pow(k, unitIndex)
  return `${value.toFixed(decimals)}${units[unitIndex]}`
}
export const ElNotificationFn = (type: 'success' | 'warning' | 'info' | 'error', title: string, message='',duration:number=3000) => {
  ElNotification[type]({
    title,
    message,
    duration
  })
}
export const formatDate = (dateStr: number) => {
  if (!dateStr||dateStr===0) return 0;
  const timestamp = String(dateStr).length === 10 ? dateStr * 1000 : dateStr;
  return dayjs(timestamp).format('YYYY-MM-DD HH:mm');
};
export const formatDateToHHMMSS = (dateStr: any) => {
  if (!dateStr||dateStr=='0') return 0;
  const timestamp = dateStr.length === 10 ? parseInt(dateStr, 10) * 1000 : parseInt(dateStr, 10);
  return dayjs(timestamp).format('YYYY-MM-DD HH:mm:ss');
};
